from 表格 import MxlsxWB
class DiscreteMath:
  xlsxwb=MxlsxWB()
  # 构造函数
  def __init__(self):
    self.min_row=1
    self.max_row=1
  # 改变读取的表格
  def set_path(self,workpath):
    self.xlsxwb.set_filename(workpath)
    self.xlsxwb.get_fileinfo()
    self.xlsxwb.choose_sheet("Sheet1")
    self.xlsxwb.get_sheetinfo()
  # 获取成绩区的第一行
  def get_first_row_of_data(self):
    self.min_row=self.xlsxwb.get_row_number_of_sheet(self.xlsxwb.get_rows_of_sheet(),"姓名")
    self.min_row+=1
    print(self.min_row)
  # 获取成绩区的最后一行
  def get_last_row_of_data(self):
    self.max_row=self.xlsxwb.get_row_number_of_sheet(self.xlsxwb.get_rows_of_sheet(),"考试/考查成绩统计")
    self.max_row-=2
    print(self.max_row)

  # 获取某学科成绩
  def get_col_of_data(self,col_name):
    self.get_first_row_of_data()
    self.get_last_row_of_data()
    col=self.xlsxwb.get_col_number_of_sheet(self.xlsxwb.num_of_rows,col_name)
    return self.xlsxwb.get_coldata_with_rownum(col,self.min_row,self.max_row)
  # 获取某个学号成绩
  def get_row_of_appoint(self,col_name,col_value):
    return self.xlsxwb.get_row_number_of_appoint_value(self.xlsxwb.num_of_rows,col_name,col_value)
  
  # 统计各分数段人数
  def histogram_of_data(self,col_data):
    if col_data is None:
      return None
    col_data.sort()
    new_data = [0 for _ in range(101)]
    for x in col_data:
      if int(x)>=0:
        new_data[int(x)]+=1
    return new_data
  
  # 统计各分数段人数，字典表示
  def histogram_of_data1(self, col_data):
    if not col_data:  # 更加Pythonic的方式检查None或空列表
        return None
    # 初始化一个字典，包含所有可能的分数作为键，初始值设为0
    score_counts = {score: 0 for score in range(101)}

    # 遍历col_data中的每个分数，并更新字典中对应的计数值
    for x in col_data:
        if 0 <= x <= 100:  # 确保分数在有效范围内
            score_counts[int(x)] += 1
    return score_counts

Demo=DiscreteMath()
Demo.set_path("d:/项目/python/爬虫/表格/信息技术成绩.xlsx")
print(Demo.get_col_of_data("数学"))
print(Demo.get_row_of_appoint("学号",2))
print(Demo.histogram_of_data(Demo.get_col_of_data("数学")))
print(Demo.histogram_of_data1(Demo.get_col_of_data("数学")))